package org.shangda.wechat.pet.util;

import com.ijpay.core.kit.WxPayKit;
import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author barry
 * @company D1M
 * @create 2020-07-19 12:23 上午
 **/
@Slf4j
public class OrderUtils {

    /**
     * 随即编码
     */

    private static final int[] r = new int[]{7, 9, 6, 2, 8, 1, 3, 0, 5, 4};
    /**
     * 用户id和随机数总长度
     */

    private static final int maxLength = 6;

    /**
     * 根据id进行加密+加随机数组成固定长度编码
     */
    private static String toCode(Integer userId) {
        String idStr = userId.toString();
        StringBuilder idsbs = new StringBuilder();
        if(maxLength > idStr.length()) {
            for (int i = idStr.length() - 1; i >= 0; i--) {
                idsbs.append(r[idStr.charAt(i) - '0']);
            }
            return idsbs.append(getRandom(maxLength - idStr.length())).toString();
        }else {
            return idsbs.append(getRandom(maxLength)).toString();
        }
    }

    /**
     * 生成时间戳
     */
    private static String getDateTime() {
        DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(new Date());
    }

    /**
     * 生成固定长度随机码
     *
     * @param n 长度
     */

    private static long getRandom(long n) {
        long min = 1, max = 9;
        for (int i = 1; i < n; i++) {
            min *= 10;
            max *= 10;
        }
        long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min;
        return rangeLong;
    }


    /**
     * 生成不带类别标头的编码
     *
     * @param userId
     */
    private static synchronized String getCode(Integer userId) {
        userId = userId == null ? 10000 : userId;
        return getDateTime() + toCode(userId);
    }


    /**
     * 生成订单单号编码(调用方法)
     * @param userId  网站中该用户唯一ID 防止重复
     */

    public static String getOrderCode(String orderPrefix,Integer userId) {
        return orderPrefix + getCode(userId);
    }


    public static void main(String[] args) {
//        System.out.println(getOrderCode("psl", 1));
//        System.out.println(getOrderCode("psl", 1));

//        System.out.println(getRandom(2));
        /*System.out.println(getDateTime());
        System.out.println(System.currentTimeMillis());
        System.out.println(getRandom(6));*/

        System.out.println(Math.abs(-1));
    }

}
